package com.estronger.shopmanage.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.utils.ImageShowUtls;
import com.estronger.shopmanage.utils.ScreenUtils;
import com.soonfor.evaluate.tools.ImageUtils;

import java.io.File;


/**
 * Created by Administrator on 2017-04-01.
 */
public class ImageFragment extends Fragment {
    private Context context;
    private ImageShowUtls imageShowUtls;
    private static final String IMAGE_URL = "image";
    ImageView image;
    private String imageUrl;
    private View.OnClickListener imgListener;
    private View.OnLongClickListener longClickListener;
    public ImageFragment() {}
    private int imageW;//屏幕宽
    private int imageH;//屏幕高

    public View.OnClickListener getImgListener() {
        return imgListener;
    }

    public void setImgListener(View.OnClickListener imgListener, View.OnLongClickListener longClickListener) {
        this.imgListener = imgListener;
        this.longClickListener = longClickListener;
    }

    public static ImageFragment newInstance(String param1) {
        ImageFragment fragment = new ImageFragment();
        Bundle args = new Bundle();
        args.putString(IMAGE_URL, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            imageUrl = getArguments().getString(IMAGE_URL);
        }
        context = getActivity();
        imageShowUtls = ImageShowUtls.getInstance(context);
        imageW = ScreenUtils.getWidthPix(context);
        imageH = ScreenUtils.getHeightPix(context);

    }

    @SuppressLint("WrongViewCast")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = null;
        if(imgListener!=null){
            view = inflater.inflate(R.layout.fragment_image, container, false);
            image = (ImageView) view.findViewById(R.id.image);
            image.setOnClickListener(imgListener);
        }else {
            view = inflater.inflate(R.layout.fragment_image_zoom, container, false);
            image = (ImageView) view.findViewById(R.id.image);
            image.setOnClickListener(imgListener);
        }
        image.setOnLongClickListener(longClickListener);
        /**
         *
         * 1.1图片质量分类

         安卓图片显示的质量配置主要分为四种:

         ARGB_8888 :32位图,带透明度,每个像素占4个字节
         ARGB_4444 :16位图,带透明度,每个像素占2个字节
         RGB_565 :16位图,不带透明度,每个像素占2个字节
         ALPHA_8 :32位图,只有透明度,不带颜色,每个像素占4个字节
         (A代表透明度,RGB代表红绿蓝:即颜色)

         1.2图片默认质量

         Picasso的默认质量是 ARGB_8888
         Glide的默认质量则为 RGB_565
         *
         * **/
        imageShowUtls.showImageWhithSize(image, imageUrl, R.mipmap.zanuw, imageW, imageH, "w");
        return view;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

//    public interface OnFragmentInteractionListener {
//        // TODO: Update argument type and name
//        void onFragmentInteraction(Uri uri);
//    }


    public String getImageUrl() {
        return imageUrl;
    }

}
